import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
symbols = [
'BICOUSDT', 'BANDUSDT', 'CELRUSDT', 'TLMUSDT', 'AIUSDT', 'NFPUSDT', 'ZILUSDT',
'DOTUSDT',
    'NEARUSDT', 'BATUSDT', 'FLOWUSDT', 'HBARUSDT', 'SANDUSDT',
    'C98USDT', 'IDUSDT',
'TRUUSDT', 'FILUSDT', 'APTUSDT', 'YGGUSDT', 'GALAUSDT', 'QTUMUSDT', 'STORJUSDT']

# FIL XLM


symbol_1 = 'FILUSDT'
symbol_2 = 'XLMUSDT'

exchange = 'binance'
data_path = Path.cwd().parent / "data" / exchange / "full_data"

mkt_1 = pd.read_csv(data_path / f'{symbol_1}-spot.csv' )
mkt_2 = pd.read_csv(data_path / f'{symbol_2}-spot.csv' )

df = pd.merge(
    mkt_1[['datetime', 'close']].rename(columns={'close': f'{symbol_1}_close'}),
    mkt_2[['datetime', 'close']].rename(columns={'close': f'{symbol_2}_close'}),
    on='datetime',
    how='inner'
)

# 计算比价
df['price_ratio'] = df[f'{symbol_1}_close'] / df[f'{symbol_2}_close']

# 计算 log spread
df['log_spread'] = (df[f'{symbol_1}_close'].apply(lambda x: np.log(x))) - (df[f'{symbol_2}_close'].apply(lambda x: np.log(x)))

# 画图
plt.figure(figsize=(14, 6))
plt.plot(df['datetime'], df['price_ratio'], label='Price Ratio (A/B)')
plt.plot(df['datetime'], df['log_spread'], label='Log Spread (log(A) - log(B))')
plt.legend()
plt.title(f'Pair Price Ratio & Log Spread: {symbol_1} vs {symbol_2}')
plt.grid()
plt.show()